﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace docmgr.WinForm
{
    public partial class UserOperation : Form
    {
        DataTable user = null;
        BLL.User userbll = new BLL.User();
        docmgr.BLL.Department departmentBll=new BLL.Department();
        docmgr.BLL.Position positionBll=new BLL.Position();
        public UserOperation()
        {
            InitializeComponent();
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(134)));

        }

        public UserOperation(String key)
        {
            InitializeComponent();
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(134)));

            user = userbll.GetModel(key);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (UserNametbx.Text == "" || Nametbx.Text == "")
            {
                MessageBox.Show("用户名和姓名不能为空，请重新输入");
                return;
            }

            try
            {
                if (user == null)
                {
                    String UserName = UserNametbx.Text;
                    String Name = Nametbx.Text;
                    String Remark = Remarktbx.Text;



                    int DepartmentID = Convert.ToInt32(Departmentcbx.SelectedValue);
                    int PositionID = Convert.ToInt32(Positioncbx.SelectedValue);

                    String Password = Common.EncryptCode("0123456789");

                    String Fileds = "UserName,Password,DepartmentID,PositionID,Remark,Name";



                    String sqlStr = String.Format("insert [User] ({0}) values ('{1}','{2}',{3},{4},'{5}','{6}')", Fileds, UserName, Password, DepartmentID, PositionID, Remark, Name);

                    SQLHelper.ExecuteSql(SQLHelper.connString, sqlStr);

                }
                else
                {
                    user.Rows[0]["UserName"] = UserNametbx.Text;
                    user.Rows[0]["Name"] = Nametbx.Text;
                    user.Rows[0]["Remark"] = Remarktbx.Text;
                    user.Rows[0]["DepartmentID"] = Departmentcbx.SelectedValue;
                    user.Rows[0]["PositionID"] = Positioncbx.SelectedValue;

                    userbll.Update(user);
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
            }
            finally
            {
                this.Close();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            user.Rows[0]["Password"] = Common.EncryptCode("0123456789");//密码重置
            userbll.Update(user);

            MessageBox.Show("密码重置成功");
            this.Close();
        }

        private void UserOperation_Load(object sender, EventArgs e)
        {
            if (user == null)
                button2.Visible = false;
            else
            {
                UserNametbx.Text = (String)user.Rows[0]["UserName"];
                Nametbx.Text = (String)user.Rows[0]["Name"];
                Remarktbx.Text = (String)user.Rows[0]["Remark"];
                Departmentcbx.SelectedValue = Convert.ToInt32(user.Rows[0]["DepartmentID"]);
                Positioncbx.SelectedValue = Convert.ToInt32(user.Rows[0]["PositionID"]);
            }
            initPoscomboBox();
            initDepcomboBox();
        }

        void initPoscomboBox()
        {
            DataSet xx = new DataSet();
            xx.Tables.Clear();
            xx.Tables.Add(positionBll.GetList("1=1"));
            this.Positioncbx.DataSource = xx.Tables[0];
            this.Positioncbx.DisplayMember = "Name";
            this.Positioncbx.ValueMember = "ID";
        }

        void initDepcomboBox()
        {
            DataSet xx = new DataSet();
            xx.Tables.Clear();
            xx.Tables.Add(departmentBll.GetList("1=1"));
            this.Departmentcbx.DataSource = xx.Tables[0];
            this.Departmentcbx.DisplayMember = "Name";
            this.Departmentcbx.ValueMember = "ID";
        }
    }
}
